open(BUFF,"markers.txt");
while($line = <BUFF>)
{
	chomp($line);	
	@ar = split(" ",$line);
	$hash{$ar[0]} = $ar[1];
}
open(BUFF,"phasedfemale.bgl");
open(BUFF1,"phasedmale.bgl");
open(TPED,">xchr.tped");
open(TFAM,">xchr.tfam");
$male = <BUFF1>;

@male = split(" ",$male);
shift(@male);
shift(@male);
for($i=0;$i<@male;$i++)
{
	print TFAM "$male[$i] $male[$i] 0 0 1 -9\n";
#	print "$male[$i] $male[$i] 0 0 -9 -9\n";
}
$female = <BUFF>;
@female = split(" ",$female);
shift(@female);
shift(@female);
for($i=0;$i<@female;$i++)
{
        print TFAM "$female[$i] $female[$i] 0 0 2 -9\n";
	$i++;
}
while($female = <BUFF>)
{
	chomp($female);
	$male = <BUFF1>;
	chomp($male);
	@male = split(" ",$male);
	@female = split(" ",$female);	
	shift(@male);
	$rsm = shift(@male);
	shift(@female);
	$rsf = shift(@female);
	if($rsm eq $rsf || exists($hash{$rsm}))
	{
		$line = "23 $rsf 0 $hash{$rsm}";
	}	
	else
	{
		die "$rsm\t$rsf\n";
	}
	for($i=0;$i<@male;$i++)
	{
		$line = "$line $male[$i] $male[$i]";
	}
	#print "$line\n";
	$rsf = join(" ",@female);
	$line = "$line $rsf";
	print TPED "$line\n";
	
}
system("perl perl_extract.pl");
system("/data1/bsi/BORA_processing/genotyping_imp_workflow_QC_V1/bin/plink/plink --tfile xchr --keep xchr_include --make-bed --out xchr_include");
system("perl perl_extract_replacetfam.pl");
system("/data1/bsi/BORA_processing/genotyping_imp_workflow_QC_V1/bin/plink/plink --bfile allpopulation --bmerge xchr_include.bed xchr_include.bim xchr_include.fam --transpose --recode --out merge");
system("mv merge.tped hapmap.tped");
